package com.buvaka.features;

import java.util.regex.*;

public class LatLng {
										
	//TODO: Design pattern for float numbers
	public static final Pattern PATERN =  Pattern.compile("^\\s*\\[\\s*[0-9]{1,3}\\s*,\\s*[0-9]{1,3}\\s*\\]\\s*$");
		
	private double lat;
	private double lng;
	
	
	public LatLng(double lat, double lng){
		this.setLat(lat);
		this.setLng(lng);
	}
	
	public LatLng(){
		this(Double.NaN, Double.NaN);
	}
	
	//GeoJson style: lng goes first
	@Override
	public String toString(){
		return new StringBuilder().append(Features.OPEN_ARRAY).append(getLng()).append(Features.COMMA).append(getLat()).append(Features.CLOSE_ARRAY).toString();
	}
	
	/**
	 * @return the lat
	 */
	public double getLat() {
		return lat;
	}

	/**
	 * @param lat the lat to set
	 */
	public void setLat(double lat) {
		//TODO: check lat range
		this.lat = lat;
	}

	/**
	 * @return the lng
	 */
	public double getLng() {
		return lng;
	}

	/**
	 * @param lng the lng to set
	 */
	public void setLng(double lng) {
		//TODO: check lng range
		this.lng = lng;
	}
	
	public static boolean isValid(String in){
		//TODO: Make validation more powerfull and informative
		return PATERN.matcher(in).matches();
	}
	
	public static LatLng parse (String in) {
		
		String[] values = new String[2];
		if (isValid(in)){
			in = in.replaceAll("\\s","");
			in = in.substring(1, in.length()-1);
			values = in.split(",");
			return new LatLng(Double.parseDouble(values[1]), Double.parseDouble(values[0]));
		}
		return new LatLng();
		
	}
	
	public double[] toArray(){
		return new double[]{lng, lat};
	}

	
	public static void main (String[] args){
	    
	    System.out.println(isValid("[12,3]"));
	    System.out.println("		[    12,3]".replaceAll("\\s",""));
	    
	    String in = "		[    12,3]  ";
	    
	    LatLng l = LatLng.parse(in);
	    
	    System.out.println(l);
		
	}
	
	
}
